package com.supor.aiot.food.adapter;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.supor.aiot.food.R;
import com.supor.aiot.food.bean.RecipeDeviceBean;
import com.supor.aiot.food.bean.RecipeHotBean;

import java.util.List;

public class CookerProductorAdapter extends RecyclerView.Adapter<CookerProductorAdapter.ProductorHolder>{
    private List<RecipeHotBean> mList;
    private Context mContext;
    private CookerProductorAdapter.OnItemClickListener recipeDeviceClick;
    private boolean hideTop;

    public void setHideTop(boolean hideTop) {
        this.hideTop = hideTop;
    }

    public CookerProductorAdapter(List<RecipeHotBean> mList, Context mContext , CookerProductorAdapter.OnItemClickListener listener) {
        this.mList = mList;
        this.mContext = mContext;
        this.recipeDeviceClick = listener;
    }

    @NonNull
    @Override
    public ProductorHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(mContext).inflate(R.layout.item_cooker_productor,parent, false);
        return new ProductorHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ProductorHolder holder, int position) {
        RecipeHotBean recipeHotBean = mList.get(position);
        LinearLayoutManager manager = new LinearLayoutManager(mContext,RecyclerView.HORIZONTAL,false);
        holder.rvMutiAvatar.setLayoutManager(manager);
        MutiAvatarAdapter avatarAdapter = new MutiAvatarAdapter(recipeHotBean.getMlist(),mContext);
        holder.rvMutiAvatar.setAdapter(avatarAdapter);

        holder.llProductOuter.setOnClickListener(view -> {
            recipeDeviceClick.OnItemClick(position);
        });
        if (!TextUtils.isEmpty(recipeHotBean.getSmallLandscape())) Glide.with(mContext).load(recipeHotBean.getImageVertical()).placeholder(new ColorDrawable(mContext.getResources().getColor(R.color.white_f5f5f5))).into(holder.ivProductor);
        if (!TextUtils.isEmpty(recipeHotBean.getRecipeName())) holder.tvProductorName.setText(recipeHotBean.getRecipeName());
        if (!TextUtils.isEmpty(recipeHotBean.getRecipeType())&&recipeHotBean.getRecipeType().equals("VIDEO")){
            holder.ivProductorPlay.setVisibility(View.VISIBLE);
        }else {
            holder.ivProductorPlay.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(recipeHotBean.getCrmModelName())){
            holder.tvProductorTag.setVisibility(View.VISIBLE);
            holder.tvProductorTag.setText(recipeHotBean.getCrmModelName());
        }else {
            holder.tvProductorTag.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(recipeHotBean.getTjValue())){
            if (recipeHotBean.getTjValue().contains(mContext.getResources().getString(R.string.percent))){
                if (recipeHotBean.getTjValue().equals(mContext.getResources().getString(R.string.zero_percent))){
                    holder.llLikeOuter.setVisibility(View.GONE);
                }else {
                    holder.llLikeOuter.setVisibility(View.VISIBLE);
                    holder.tvLikeSize.setText(recipeHotBean.getTjValue()+mContext.getResources().getString(R.string.like_precent));
                }
            }else {
                holder.llLikeOuter.setVisibility(View.VISIBLE);
                holder.tvLikeSize.setText(recipeHotBean.getTjValue()+mContext.getResources().getString(R.string.percent)+mContext.getResources().getString(R.string.like_precent));
            }
        }

        if (!hideTop&&recipeHotBean.getIsChefOne()==1){
            holder.setTop.setVisibility(View.VISIBLE);
        } else {
            holder.setTop.setVisibility(View.GONE);
        }

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class ProductorHolder extends RecyclerView.ViewHolder{
        public ImageView ivProductor;
        public TextView tvProductorName;
        public TextView tvProductorTag;
        public ImageView ivProductorPlay;
        public RecyclerView rvMutiAvatar;
        public TextView tvLikeSize;
        public TextView setTop;
        public LinearLayout llProductOuter;
        public LinearLayout llLikeOuter;

        public ProductorHolder(@NonNull View itemView) {
            super(itemView);
            ivProductor=itemView.findViewById(R.id.iv_productor_bg);
            tvProductorName=itemView.findViewById(R.id.tv_device_name);
            tvProductorTag=itemView.findViewById(R.id.tv_productor_tag);
            ivProductorPlay=itemView.findViewById(R.id.iv_productor_play);
            rvMutiAvatar=itemView.findViewById(R.id.rv_liked_avatar);
            tvLikeSize=itemView.findViewById(R.id.tv_productor_like);
            llProductOuter=itemView.findViewById(R.id.ll_productor_outer);
            setTop=itemView.findViewById(R.id.tv_set_top);
            llLikeOuter=itemView.findViewById(R.id.ll_like_outer);
        }
    }

    public interface OnItemClickListener {
        void OnItemClick(int position);
    }

}
